/*
 * @(#) StringUtil.java 2011-4-21
 *
 * Copyright (c) 2010, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.common.util;

import java.io.UnsupportedEncodingException;
import java.text.NumberFormat;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 字符串处理类
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-4-21
 */
public class StringUtil
{
	private final static NumberFormat numberFormat;
	static
	{
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);
		nf.setGroupingUsed(false);
		numberFormat = nf;
	}

	/**
	 * 获取随即序列
	 * 
	 * @return
	 */
	public static String getUUID()
	{
		return UUID.randomUUID().toString().replaceAll("_", "");
	}

	/**
	 * null to ""
	 * 
	 * @param s
	 * @return
	 */
	public static String nvl(String s)
	{
		return s == null ? "" : s;
	}

	/**
	 * 去除前后空格
	 * 
	 * @param sIn
	 * @return
	 */
	public static String nvlTrim(String sIn)
	{
		return nvl(sIn).trim();
	}

	/**
	 * null or "" 判断
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s)
	{
		if (s == null || "".equals(s))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Replaces each substring of this string that matches the given regular expression with the given replacement.
	 * 
	 * @param s 源字符串
	 * @param regex 被替换字符串
	 * @param replacement 替换字符串
	 * @return 替换后字符串
	 */
	public static String replaceAll(String s, String regex, String replacement)
	{
		if (s == null)
		{
			return null;
		}
		if ((regex == null) || (replacement == null))
		{
			return s;
		}
		int beginIdx = 0;
		int regexLen = regex.length();
		int idx = s.indexOf(regex);
		StringBuffer sb = new StringBuffer("");
		while (idx >= 0)
		{
			sb.append(s.substring(beginIdx, idx));
			sb.append(replacement);
			beginIdx = idx + regexLen;
			idx = s.indexOf(regex, beginIdx);
		}
		sb.append(s.substring(beginIdx));
		return sb.toString();
	}

	/**
	 * 消息参数替换
	 * 
	 * @param msg 变换前的消息
	 * @param replacementArray 参数
	 * @return 变换后的消息
	 */
	public static String setReplacement4Msg(String msg, String[] replacementArray)
	{
		if (replacementArray != null && !StringUtil.isEmpty(msg))
		{
			for (int i = 0; i < replacementArray.length; i++)
			{
				String replacement = StringUtil.nvl(replacementArray[i]);
				String replaceKey = "{" + i + "}";

				msg = replaceAll(msg, replaceKey, replacement);
			}
		}
		return msg;
	}

	/**
	 * 获取字符串长度
	 * 
	 * @param str
	 * @return
	 */
	public static int getLen(String str)
	{
		if (isEmpty(str))
		{
			return 0;
		}

		int len = 0;
		for (int i = 0; i < str.length(); i++)
		{
			char ch = str.charAt(i);

			if (!(ch > 255))
			{
				len = len + 1;
			}
			else
			{
				len = len + 2;
			}
		}
		return len;
	}

	/**
	 * 判断字符串是否符合某个表达式
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	private static boolean matchRegex(String str, String regex)
	{
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches())
		{
			return true;
		}
		return false;
	}

	/**
	 * 判断邮件格式是否正确
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email)
	{
		String emailRegex = Constants4Regex.EMAIL;
		return matchRegex(email, emailRegex);
	}

	/**
	 * 判断手机格式是否正确
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean isPhone(String phone)
	{
		String phoneRegex = Constants4Regex.PHONE;
		return matchRegex(phone, phoneRegex);
	}

	/**
	 * 判断电话格式是否正确
	 * 
	 * @param tel
	 * @return
	 */
	public static boolean isTel(String tel)
	{
		String telRegex = Constants4Regex.TEL;
		return matchRegex(tel, telRegex);
	}

	/**
	 * 判断是否是字符、数字或下划线
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumberOr_Letter(String str)
	{
		String regex = Constants4Regex.NUMBEROR_LETTER;
		return matchRegex(str, regex);
	}

	/**
	 * 是否是数字开头
	 * @param str
	 * @return
	 */
	public static boolean isStartWithNumber(String str)
	{
		Pattern regex = Pattern.compile(Constants4Regex.START_WHIT_NUMBER);
		Matcher matcher = regex.matcher(String.valueOf(str.trim()));
		boolean isMatch = matcher.matches();
		return isMatch;
	}
	/**
	 * 判断是否汉字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isChinese(String str)
	{
		String regex = Constants4Regex.IS_CH;
		return matchRegex(str, regex);
	}

	/**
	 * 判断是否是字母数字及指定符号
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumberLetterMarks(String str)
	{
		String regex = Constants4Regex.NUMBER_LETTER_MARKS;
		return matchRegex(str, regex);
	}

	/**
	 * 判断是否全是数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str)
	{
		String regex = Constants4Regex.NUMBER;
		return matchRegex(str, regex);
	}
	
	/**
	 * 判断是否全是数字或小数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumberOrDecimal(String str)
	{
		String regex = Constants4Regex.NUMBERORDECIMAL;
		return matchRegex(str, regex);
	}
	/**
	 * 数字格式化
	 * 
	 * @param num
	 * @return
	 */
	public static String getNumberDisplay(Number num)
	{
		if (num == null)
		{
			return "";
		}
		return numberFormat.format(num);
	}

	/**
	 * seq位数不够在前面添加0
	 * 
	 * @param seq 采番号
	 * @param count
	 * @return
	 */
	public static String addZero(String seq, int count)
	{
		// 判断seq的位数是否符合
		if (seq.length() == count)
		{
			return seq;
		}
		else
		{
			StringBuffer buf = new StringBuffer();
			for (int i = 0; i < count - seq.length(); i++)
			{
				buf.append("0");
			}
			buf.append(seq);
			return buf.toString();
		}
	}

	/**
	 * 
	 * @param srcStr
	 * @return
	 */
	public static String strEncoding(String srcStr)
	{
		try
		{
			return new String(srcStr.getBytes("ISO-8859-1"), "UTF-8");
		}
		catch (UnsupportedEncodingException e)
		{
			return srcStr;
		}
	}

	/**
	 * 
	 * @param srcStr
	 * @return
	 */
	public static String strDecoding(String srcStr)
	{
		try
		{
			return new String(srcStr.getBytes("GBK"), "ISO8859-1");
		}
		catch (UnsupportedEncodingException e)
		{
			return srcStr;
		}
	}
}
